﻿namespace FFF.Test.Workers
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Transactions;
    using Abp.BackgroundJobs;
    using Abp.Dependency;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore;
    using Abp.Threading;
    using Abp.Threading.BackgroundWorkers;
    using Abp.Threading.Timers;
    using Microsoft.EntityFrameworkCore;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Util.Consts;
    using Ydm.Helper;

    /// <summary>
    /// 定时任务处理
    /// </summary>
    public class MyWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
    {
        /// <summary>
        ///  数据库对象,可以执行sql语句
        /// </summary>
        private readonly IDbContextProvider<MyDbContext> _dbContextProvider;

        private readonly IBackgroundJobManager _backgroundJobManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="MyWorker"/> class.
        /// 构造注入
        /// </summary>
        /// <param name="timer">时间配置</param>
        /// <param name="dbContextProvider">db</param>
        /// <param name="backgroundJobManager">后台工人</param>
        public MyWorker(
            AbpTimer timer,
            IDbContextProvider<MyDbContext> dbContextProvider,
            IBackgroundJobManager backgroundJobManager)
            : base(timer)
        {
            this._dbContextProvider = dbContextProvider;
            base.Timer.Period = (int)TimeSpan.FromMinutes(30).TotalMilliseconds;
            this._backgroundJobManager = backgroundJobManager;
        }

        ///// <summary>
        ///// 开始作业
        ///// </summary>
        //public override void Start()
        //{
        //    this.DoWork();
        //}

        /// <summary>
        /// 开始工作 30分钟没有发消息的设置为离线状态
        /// </summary>
        [UnitOfWork(isolationLevel: IsolationLevel.ReadUncommitted)]
        protected override void DoWork()
        {
            //var mail = new SendEmailJobArgs();
            //mail.Subject = "You have a new task2!";
            //mail.Body = "A new task is assigned for you: <b>123</b>";
            //mail.IsBodyHtml = true;
            //mail.Tos.Add("mingjay100@163.com");
            //mail.Tos.Add("418146370@qq.com");
            //mail.CCs.Add("4181463719999@qq.com");
            //mail.CCs.Add("yudaming@whcewei.com");
            //this._backgroundJobManager.Enqueue<SendEmailJob, SendEmailJobArgs>(mail);

            //var ssms = new SendSsmsJobArgs();
            //ssms.Phone = "15629021352";
            //ssms.TemplateCode = "SMS_166372385";
            //ssms.TemplateParamJson = new { code = "123456" };

            //this._backgroundJobManager.Enqueue<SendSsmsJob, SendSsmsJobArgs>(ssms);

            MyDbContext db = this._dbContextProvider.GetDbContext();
            string sql = @"UPDATE us
                    SET [status] = 'offline'
                    FROM layim_user us
                        OUTER APPLY
                    (
                        SELECT MAX(addtime) AS maxTime
                        FROM dbo.layim_chat_record
                        WHERE (from_id = us.id)
                    ) t
                    WHERE DATEADD(S, t.maxTime, '1970-01-01 08:00:00') < DATEADD(HOUR, -1, GETDATE()) AND [us].[status]='online'";
            AsyncHelper.RunSync(() => db.ExecuteAsync(sql));
        }
    }
}
